[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Tabs.Tabs.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Tabs.Tabs.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Tabs
{    
    using System.Collections.ObjectModel;
    using System.Web.UI;

    using AjaxControlToolkit;

    using Helper;

    /// <summary>
    /// The control that collects and manipulates tabs 
    /// </summary>
    [ParseChildren(true, "Tabs")]
    [ClientScriptResource("BM.TabContainer", "BM.Tools.WebControls.MForm.Controls.Tabs.Tabs.js")]
    public class TabContainer : ScriptControlBase
    {
        private const string TabContainerCssClass = "MForm-TabC";

        /// <summary>
        /// Initializes a new instance of the <see cref="TabContainer"/> class.
        /// </summary>
        public TabContainer()
        {
            Tabs = new ControlCollection<Tab>(Controls);
        }

        /// <summary>
        /// Gets or sets SelectedTabIndex.
        /// </summary>
        /// <value>
        /// The selected tab index.
        /// </value>
        public int SelectedTabIndex
        {
            get;
            set;
        }

        /// <summary>
        /// Gets Tabs.
        /// </summary>
        /// <value>
        /// The tabs of the container.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public Collection<Tab> Tabs
        {
            get;
            private set;            
        }

        /// <summary>
        /// Gets div TagKey.
        /// </summary>
        /// <value>
        /// The div tag key.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// sets the tab selected and the other tabs - deselected
        /// </summary>
        /// <param name="selectedTab">
        /// The selected tab.
        /// </param>
        public void Select(Tab selectedTab)
        {
            if (!Tabs.Contains(selectedTab))
            {
                return;
            }

            foreach (Tab tab in Tabs)
            {
                tab.Selected = false;
            }

            selectedTab.Selected = true;
        }

        /// <summary>
        /// Adds HTML attributes and styles that need to be rendered to the specified <see cref="T:System.Web.UI.HtmlTextWriterTag"></see>. This method is used primarily by control developers.
        /// </summary>
        /// <param name="writer">
        /// A <see cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output stream to render HTML content on the client. 
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, TabContainerCssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
        }

        /// <summary>
        /// performs the selection and renders the controls
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (SelectedTabIndex < Tabs.Count && SelectedTabIndex >= 0)
            {
                Select(Tabs[SelectedTabIndex]);
            }

            base.Render(writer);
        }
    }
}